worker_processes 2;
user nginx;

http {
  include mime.types;
  charset utf-8;

  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  client_max_body_size 500m;
  client_body_buffer_size 128k;
  proxy_connect_timeout 90;
  proxy_send_timeout 90;
  proxy_read_timeout 90;
  proxy_buffers 32 4k;

  real_ip_header CF-Connecting-IP;

  set_real_ip_from 103.21.244.0/22;
  set_real_ip_from 103.22.200.0/22;
  set_real_ip_from 103.31.4.0/22;
  set_real_ip_from 104.16.0.0/13;
  set_real_ip_from 104.24.0.0/14;
  set_real_ip_from 108.162.192.0/18;
  set_real_ip_from 131.0.72.0/22;
  set_real_ip_from 141.101.64.0/18;
  set_real_ip_from 162.158.0.0/15;
  set_real_ip_from 172.64.0.0/13;
  set_real_ip_from 173.245.48.0/20;
  set_real_ip_from 188.114.96.0/20;
  set_real_ip_from 190.93.240.0/20;
  set_real_ip_from 197.234.240.0/22;
  set_real_ip_from 198.41.128.0/17;
  set_real_ip_from 2400:cb00::/32;
  set_real_ip_from 2606:4700::/32;
  set_real_ip_from 2803:f800::/32;
  set_real_ip_from 2405:b500::/32;
  set_real_ip_from 2405:8100::/32;
  set_real_ip_from 2a06:98c0::/29;
  set_real_ip_from 2c0f:f248::/32;

  server {
    listen ${NGINX_PORT};
    server_name ${API_HOST};

    location /ntp/ {
      add_header Access-Control-Allow-Origin *;
      return 200 $msec;
    }

    location /search/ {
      proxy_pass http://meili:7700/;
    }

    location /resources/libraries/ {
      proxy_pass http://resources-service:${APP_PORT}/libraries/;
    }

    location /resources/reindex/ {
      proxy_pass http://resources-service:${APP_PORT}/reindex/;
    }

    location /resources/tags/ {
      proxy_pass http://resources-service:${APP_PORT}/tags/;
    }

    location /resources/comments/ {
      proxy_pass http://resources-service:${APP_PORT}/comments/;
    }

    location /resources/ups/ {
      proxy_pass http://resources-service:${APP_PORT}/ups/;
    }

    location /resources/reports/ {
      proxy_pass http://resources-service:${APP_PORT}/reports/;
    }

    # location /bot/ {
    #   proxy_pass http://bot-service:${APP_PORT}/;
    # }

    location /servers/ {
      proxy_pass http://resources-service:${APP_PORT}/servers/;
    }

    location /bots/ {
      proxy_pass http://resources-service:${APP_PORT}/bots/;
    }

    location /search-key/ {
      proxy_pass http://search-service:${APP_PORT}/;
    }

    location /users/ {
      proxy_pass http://users-service:${APP_PORT}/;
    }

    error_page 404 = @tidumaltutbudet;

    location @tidumaltutbudet {
      return 301 https://www.youtube.com/watch?v=PkT0PJwy8mI;
    }
  }

  server {
    listen ${NGINX_PORT};
    server_name ${GATEWAY_HOST};

    location /rpc/publish/ {
      proxy_pass http://notifications-service:${APP_PORT}/rpc/publish/;
    }

    location / {
      return 404 "Not Found";
    }

    location /websocket/ {
      proxy_pass http://notifications-service:${APP_PORT}/websocket/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_read_timeout 86400s;
    }

    error_page 404 = @tidumaltutbudet;

    location @tidumaltutbudet {
      return 301 https://www.youtube.com/watch?v=PkT0PJwy8mI;
    }
  }

  server {
    listen ${NGINX_PORT};
    server_name ${ADMINER_HOST};

    location / {
      proxy_pass http://adminer:8080/;
    }
  }

  server {
    listen ${NGINX_PORT};
    server_name ${CDN_HOST};

    location / {
      proxy_pass http://cdn-service:${APP_PORT}/;
    }
  }
}

events {
  use epoll;
  worker_connections 2048;
}
